﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lib.CommonForm;

namespace AUI
{
    public partial class frmCustomersList : FormBase
    {
        int _CustomerId = 0;

        public int CustomerId
        {
            get { return _CustomerId; }
            set { _CustomerId = value; }
        }

        string _CustomerCode;

        public string CustomerCode
        {
            get { return _CustomerCode; }
            set { _CustomerCode = value; }
        }

        string _CustomerName;

        public string CustomerName
        {
            get { return _CustomerName; }
            set { _CustomerName = value; }
        }


        DataTable _dt = null;
        public frmCustomersList()
        {
            //Dành cho admin
            InitializeComponent();
            Initialize();
            this._isSelect = true;
            this.Text = "Chọn khách hàng (Ctrl + Enter)";
            btnSelect.Visible = _isSelect;
            cboBranch.ComboBox.SelectedValue = FormGlobals.BranchID;
            ShowLoadData();
            _IsLoading = false;
        }
        public frmCustomersList(string formText, bool isSelect, int branchID, string keyWord)
        {
            InitializeComponent();
            Initialize();
            if (formText != "")
            {
                this.Text = formText;
                this.TabText = formText;
            }
            this._isSelect = isSelect;
            this._keyWord = keyWord;
            this._branchId = branchID;
            btnSelect.Visible = _isSelect;
            if (isSelect)
            {
                this.Text = this.Text + " (Ctrl + Enter)";
                txtKeyword.Text = keyWord;
                cboBranch.Enabled = false;
                cboBranch.ComboBox.SelectedValue = _branchId;
            }

            ShowLoadData();
            _IsLoading = false;
        }
        public override void Show_Form()
        {
            this.Show();
        }
        void Initialize()
        {
            

            _listRole = tbl_RoleRight_Controller.Instance().GetInfo("KhachHang");

            FormGlobals.dgv_SelectionMode_FullRowSelect(dgvList);
            FormGlobals.dgv_AutoSizeColumnsMode_BeginLoad(dgvList);
            dgvList.BorderStyle = BorderStyle.FixedSingle;

            TextBoxFind tbf = new TextBoxFind(txtKeyword, "");
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "CustomerCode", "Mã NCC", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "CustomerName", "Tên NCC", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "Address", "Địa chỉ", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "Email", "Email", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "Phone", "Điện thoại", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "Mobile", "Di động", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "Fax", "Fax", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "BranchName", "Đơn vị", "", 1, true, true);
            FormGlobals.dgv_AddColumn_TextBox(dgvList, "CustomerId", "CustomerId", "", 1, false, true);

            FormGlobals.dgv_ColumnsSortMode(dgvList, DataGridViewColumnSortMode.Programmatic);


            LoadComboBox.BranchID_Filter(cboBranch);
            this.Activated += new EventHandler(frmUserList_Activated);
            dgvList.SelectionChanged += new EventHandler(dgvList_SelectionChanged);
        }

        void dgvList_SelectionChanged(object sender, EventArgs e)
        {
            if (_IsLoading) return;

            if (dgvList.SelectedRows.Count > 0)
            {
                _CustomerId = Convert.ToInt32(dgvList.SelectedRows[0].Cells["CustomerId"].Value);
                _CustomerCode = Convert.ToString(dgvList.SelectedRows[0].Cells["CustomerCode"].Value);
                _CustomerName = Convert.ToString(dgvList.SelectedRows[0].Cells["CustomerName"].Value);
            }
        }

        void frmUserList_Activated(object sender, EventArgs e)
        {
            if (_isSelect)
            {
                //txtNo.Focus();
            }
        }
        private void ShowLoadData()
        {
            _IsLoading = true;
            _keyWord = Globals.GetKeyWord(txtKeyword.Text);
            _branchId = Globals.ToInt32(cboBranch.ComboBox.SelectedValue);

            Splash.Show(LoadData, "");
            dgvList.DataSource = _dt;
            PageIndex(lblPageIndex, lblTotalRows);

            _IsLoading = false;
        }
        private void LoadData()
        {
            //Run in thread
            _dt = tbl_Customers_Controller.Instance().GetList(out _itemCount, _orderBy, _orderDirection, _currentPage, _pageSize, _keyWord, _branchId);
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                Form f = new frmCustomerInfo(FormGlobals.dgv_GetDataRow(dgvList));
                if (f.ShowDialog(this) == DialogResult.OK)
                {
                    ShowLoadData();
                    FormGlobals.dgv_ReSelection(dgvList, "CustomerId", _CustomerId);
                }
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Form f = new frmCustomerInfo(null);
            if (f.ShowDialog(this) == DialogResult.OK)
            {
                _currentPage = 0;
                ShowLoadData();
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {
                if (FormGlobals.Message_Confirm(UIMessage.QUESTION_DELETE))
                {
                    if (tbl_Customers_Controller.Instance().Delete(_CustomerId))
                    {
                        FormGlobals.Message_InTaskbar(UIMessage.DETETE_COMPLETE);
                        ShowLoadData();
                    }
                    else
                        FormGlobals.Message_Error(new Exception( UIMessage.DETETE_ERROR));
                }
            }
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
                if (_isSelect)
                    btnSelect.PerformClick();
                else
                    if (btnEdit.Enabled)
                        btnEdit.PerformClick();
        }


        private void btnFirst_Click(object sender, EventArgs e)
        {
            if (_currentPage > 0)
            {
                _currentPage = 0;
                ShowLoadData();
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (_totalPage > 0 && _currentPage < _totalPage - 1)
            {
                _currentPage++;
                ShowLoadData();
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            if (_currentPage > 0)
            {
                _currentPage--;
                ShowLoadData();
            }
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            if (_totalPage > 0 && _currentPage < _totalPage - 1)
            {
                _currentPage = _totalPage - 1;
                ShowLoadData();
            }
        }

        void Refresh()
        {
            if (!_IsLoading)
            {
                _currentPage = 0;
                ShowLoadData();
            }
        }

        private void btnSearh_Click(object sender, EventArgs e)
        {
            Refresh();
        }


        private void cboBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!_IsLoading)
            {
                Refresh();
            }
        }

        private void cboDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            Refresh();
        }

        private void frmUserList_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.Down:
                    if (dgvList.Rows.Count > 0)
                    {
                        dgvList.Focus();
                    }
                    break;
                case Keys.Insert:
                case Keys.Control | Keys.Enter:
                    btnSelect.PerformClick();
                    break;
            }
        }


        private void txtKeyword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
                Refresh();
        }

        private void dgvList_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dgv_ColumnHeaderMouseClick(sender, e, delegate { ShowLoadData(); });
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            Refresh();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            if (!FormGlobals.Message_Confirm("Bạn có chắc chắn " + btnExcel.Text + "?")) return;
            try
            {
                int out1 = 0;
                DataTable dtExcel = tbl_Customers_Controller.Instance().GetList(out out1, _orderBy, _orderDirection, 0, _itemCount, _keyWord, _branchId);
                ReadExcelFile.MSExcelExport(FormGlobals.dgv_GetColumnHeaderText(dgvList, dtExcel), this.Text);
            }
            catch (Exception ex)
            {
                FormGlobals.Message_Warning(ex.Message);
                return;
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
            {

                _dlgResult = DialogResult.OK;
                this.Close();
            }
        }

        private void txtNo_KeyDown(object sender, KeyEventArgs e)
        {

        }

    }
}
